package com.gamecollection.snake.utils;

import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 游戏工具类
 */
public class GameUtils {
    
    // 蛇头图片
    public static Image upImg;
    public static Image downImg;
    public static Image leftImg;
    public static Image rightImg;
    
    // 蛇身体图片
    public static Image bodyImg;
    
    // 食物图片
    public static Image foodImg;
    
    // 关卡
    public static int level = 1;
    
    // 静态初始化块
    static {
        try {
            upImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/up.png"));
            downImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/down.png"));
            leftImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/left.png"));
            rightImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/right.png"));
            bodyImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/body.png"));
            foodImg = Toolkit.getDefaultToolkit().getImage(GameUtils.class.getResource("/img/snake/food.png"));
        } catch (Exception e) {
            System.err.println("Failed to load images: " + e.getMessage());
            // 创建默认的彩色矩形作为替代
            upImg = createDefaultImage(Color.RED);
            downImg = createDefaultImage(Color.BLUE);
            leftImg = createDefaultImage(Color.GREEN);
            rightImg = createDefaultImage(Color.YELLOW);
            bodyImg = createDefaultImage(Color.GRAY);
            foodImg = createDefaultImage(Color.ORANGE);
        }
    }
    
    /**
     * 创建默认图片
     */
    private static Image createDefaultImage(Color color) {
        BufferedImage img = new BufferedImage(30, 30, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = img.createGraphics();
        g2d.setColor(color);
        g2d.fillRect(0, 0, 30, 30);
        g2d.dispose();
        return img;
    }
    
    /**
     * 绘制文字
     */
    public static void drawWord(Graphics g, String str, Color color, int size, int x, int y) {
        g.setColor(color);
        g.setFont(new Font("仿宋", Font.BOLD, size));
        g.drawString(str, x, y);
    }
} 